﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace FileExplorerSidebarPlugin
{
    public partial class Explorer : UserControl
    {
        ImageComboBox systemDrives;
        private String CurrentDirectory;
        public Explorer()
        {
            InitializeComponent();
            systemDrives= new ImageComboBox();
            this.splitContainer1.Panel1.Controls.Add(this.systemDrives);
            this.systemDrives.Dock = System.Windows.Forms.DockStyle.Fill;
            this.systemDrives.ImageList = imageDriverList;
            this.systemDrives.FormattingEnabled = true;
            this.systemDrives.Location = new System.Drawing.Point(0, 0);
            this.systemDrives.DropDownStyle = ComboBoxStyle.DropDownList;

            this.systemDrives.Name = "systemDrives";
            this.systemDrives.TabIndex = 0;
            this.systemDrives.Size = new System.Drawing.Size(180, 21);
            this.systemDrives.SelectedValueChanged += new EventHandler(systemDrives_SelectedValueChanged);
        }

        void systemDrives_SelectedValueChanged(object sender, EventArgs e)
        {
            CurrentDirectory = systemDrives.SelectedItem.ToString();
            UpdateDirectory();
        }

        private void UserControl1_Load(object sender, EventArgs e)
        {
            foreach (System.IO.DriveInfo Current in System.IO.DriveInfo.GetDrives())
            {
                if(Current.IsReady){
                int imgIndex;
                switch (Current.DriveType)
                {
                    case System.IO.DriveType.Fixed:
                        imgIndex = 0;
                        break;
                    case System.IO.DriveType.Removable:
                        imgIndex = 0;
                        break;
                    case System.IO.DriveType.CDRom:
                        imgIndex = 1;
                        break;
                    default:
                        imgIndex = 0;
                        break;
                }
                systemDrives.Items.Add(new ImageComboBoxItem(Current.RootDirectory.ToString(), imgIndex));
                }
            }

            _Open.Text = _MenuOpen.Text = _MenuOpen.ToolTipText = Notepad_X.Main.app_language.GetString(Notepad_X.Classes.Strings.Open);

            _Delete.Text = _MenuDelete.Text = _MenuDelete.ToolTipText = Notepad_X.Main.app_language.GetString(Notepad_X.Classes.Strings.Remove);


            if (Notepad_X.Static.MainInstance.IconsCollection.Images.ContainsKey("open"))
            {
                _Open.Image = _MenuOpen.Image = Notepad_X.Static.MainInstance.IconsCollection.Images["open"];
            
                iconsList.Images.Add("folder", Notepad_X.Static.MainInstance.IconsCollection.Images["open"]);
            }
            if (Notepad_X.Static.MainInstance.IconsCollection.Images.ContainsKey("file"))
            {
                iconsList.Images.Add("file", Notepad_X.Static.MainInstance.IconsCollection.Images["file"]);
            }
            if (Notepad_X.Static.MainInstance.IconsCollection.Images.ContainsKey("remove"))
            {
                _Delete.Image = _MenuDelete.Image = Notepad_X.Static.MainInstance.IconsCollection.Images["remove"];
            }

        
        }
        private void UpdateDirectory()
        {
            fileList.Items.Clear();
            if (CurrentDirectory != System.IO.Directory.GetDirectoryRoot(CurrentDirectory))
            {
                FolderListViewItem newDir = new FolderListViewItem("...");
                fileList.Items.Add(newDir);
                newDir.ImageKey = "folder";
            }
            foreach (string Dir in System.IO.Directory.GetDirectories(CurrentDirectory))
            {
                
                FolderListViewItem newDir = new FolderListViewItem(new System.IO.DirectoryInfo(Dir).Name);
                fileList.Items.Add(newDir);
                newDir.ImageKey = "folder";
            }


            foreach (string File in System.IO.Directory.GetFiles(CurrentDirectory))
            {
                FileListViewItem newFile = new FileListViewItem(System.IO.Path.GetFileName(File));
                fileList.Items.Add(newFile);
                newFile.ImageKey = "file";

                // Get Icon File for Each Extension
                //String Extension = System.IO.Path.GetExtension(File);
                //if (!String.IsNullOrEmpty(Extension))
                //{
                //    if (iconsList.Images.ContainsKey(Extension))
                //    {
                //        newFile.ImageKey = Extension;
                //    }
                //    else
                //    {
                //        iconsList.Images.Add(Extension, Icon.ExtractAssociatedIcon(File));
                //        newFile.ImageKey = Extension;
                //    }
                //}
            }
        }
        private void OpenListItem()
        {
            if (fileList.SelectedItems[0].GetType() == typeof(FolderListViewItem))
            {
                if (fileList.SelectedItems[0].Text == "...")
                {
                    CurrentDirectory = System.IO.Directory.GetParent(CurrentDirectory).FullName;
                }else{
                    CurrentDirectory += "\\" + fileList.SelectedItems[0].Text;
                }

                UpdateDirectory();
            }else if(fileList.SelectedItems[0].GetType() == typeof(FileListViewItem)){
                Notepad_X.Static.MainInstance.system_open(CurrentDirectory + "\\" + fileList.SelectedItems[0].Text);
            }
        }
        private void fileList_ItemActivate(object sender, EventArgs e)
        {
            OpenListItem();
        }
      

        private void fileList_SelectedIndexChanged(object sender, EventArgs e)
        {
            Boolean Enabled = true;
            if (fileList.SelectedItems.Count == 0)
            {
                Enabled = false;
            }
            _MenuOpen.Enabled = Enabled;
            _Open.Enabled = Enabled;
            _MenuDelete.Enabled = Enabled;
            _Delete.Enabled = Enabled;
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (fileList.SelectedItems.Count > 0)
            {
                OpenListItem();
            }
        }

        private void _MenuDelete_Click(object sender, EventArgs e)
        {
           
            if (fileList.SelectedItems[0].GetType() == typeof(FolderListViewItem))
            {
                MessageBox.Show(fileList.SelectedItems[0].Text);
                try
                {
                    System.IO.Directory.Delete(System.IO.Path.Combine(CurrentDirectory, fileList.SelectedItems[0].Text), true);
                    fileList.Items.Remove(fileList.SelectedItems[0]);
                    fileList.Update();
                    
                }catch(Exception){
                }
            }else if (fileList.SelectedItems[0].GetType() == typeof(FileListViewItem)){
                try
                {
                    System.IO.File.Delete(System.IO.Path.Combine(CurrentDirectory, fileList.SelectedItems[0].Text));
                    int index = fileList.SelectedItems[0].Index;
                    fileList.Items.Remove(fileList.SelectedItems[0]);
                    fileList.Update();
                    fileList.RedrawItems(index-1, fileList.Items.Count - 1, false);
                }
                catch (Exception)
                {
                }
            }
        }

     


    }
}
